package com.feature.shop.user.service;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.feature.core.domain.PageVo;
import com.feature.core.mybatis.BaseService;
import com.feature.core.mybatis.SmartBaseMapper;
import com.feature.core.util.AssertUtil;
import com.feature.shop.auth.api.vo.LoginUserVo;
import com.feature.shop.user.api.request.ShopRoleAddRequest;
import com.feature.shop.user.api.request.ShopRoleDelRequest;
import com.feature.shop.user.api.request.ShopRoleListRequest;
import com.feature.shop.user.api.request.ShopRolePageRequest;
import com.feature.shop.user.api.request.ShopRoleUpdateRequest;
import com.feature.shop.user.api.vo.ShopRoleVo;
import com.feature.shop.user.converter.ShopRoleConverter;
import com.feature.shop.user.mapper.ShopAuthMapper;
import com.feature.shop.user.mapper.ShopRoleAuthMapper;
import com.feature.shop.user.mapper.ShopRoleMapper;
import com.feature.shop.user.mapper.ShopUserRoleMapper;
import com.feature.shop.user.model.entity.ShopAuthEntity;
import com.feature.shop.user.model.entity.ShopRoleAuthEntity;
import com.feature.shop.user.model.entity.ShopRoleEntity;
import com.feature.shop.user.model.entity.ShopUserRoleEntity;

import io.jsonwebtoken.lang.Arrays;

/***
 * 角色Service
 */
@Service
public class ShopRoleService extends BaseService<ShopRoleEntity> {
    @Autowired
    private ShopRoleMapper shopRoleMapper;

    @Autowired
    private ShopRoleConverter shopRoleConverter;

    @Autowired
    ShopAuthMapper shopAuthMapper;

    @Autowired
    ShopAuthService shopAuthService;

    @Autowired
    ShopRoleAuthMapper shopRoleAuthMapper;

    @Autowired
    ShopUserRoleMapper shopUserRoleMapper;

    @Override
    public SmartBaseMapper<ShopRoleEntity> getSmartBaseMapper() {
        return shopRoleMapper;
    }

    public PageVo<ShopRoleVo> findPageList(ShopRolePageRequest req) {
        PageVo<ShopRoleEntity> pageData = shopRoleMapper.selectPageList(req.getCurPage(), req.getPageSize(), shopRoleConverter.pageRequestToQuery(req));
        PageVo<ShopRoleVo> vo = PageVo.of(pageData, shopRoleConverter::entityListToVO);
        if (!vo.getItems().isEmpty()) {
            for (ShopRoleVo roleVo : vo.getItems()) {
                List<ShopAuthEntity> authList = shopRoleAuthMapper.getAuthByRoleId(roleVo.getId());
                roleVo.setAuthPaths(authList.stream().map(ShopAuthEntity::getPath).collect(Collectors.toSet()));
            }
        }
        return vo;

    }

    public List<ShopRoleVo> findList(ShopRoleListRequest req) {
        List<ShopRoleEntity> list = shopRoleMapper.selectList(shopRoleConverter.listRequestToQuery(req));
        return shopRoleConverter.entityListToVO(list);
    }

    @Override
    public Class<ShopRoleEntity> getEntityClass() {
        return ShopRoleEntity.class;
    }

    public void addRole(ShopRoleAddRequest req, LoginUserVo loginUser) {
        // 校验名称重复
        ShopRoleEntity queryCount = new ShopRoleEntity();
        queryCount.setName(req.getRoleName());
        long count = shopRoleMapper.selectCount(queryCount);
        AssertUtil.isTrue(count == 0, "名称已存在");
        ShopRoleEntity role = new ShopRoleEntity();
        role.setName(req.getRoleName());
        role.setCreateUser(loginUser.getUser().getId());
        role.setUpdateUser(loginUser.getUser().getId());
        shopRoleMapper.insert(role);
        if (StringUtils.isNotBlank(req.getAuthPaths())) {
            List<String> authPathList = Arrays.asList(req.getAuthPaths().split(","));
            List<ShopAuthEntity> authList = shopAuthService.getByPaths(authPathList);
            AssertUtil.isTrue(authList.size() == authPathList.size(), "权限不存在");
            for (ShopAuthEntity auth : authList) {
                ShopRoleAuthEntity roleAuth = new ShopRoleAuthEntity();
                roleAuth.setAuthId(auth.getId());
                roleAuth.setCreateUser(loginUser.getUser().getId());
                roleAuth.setRoleId(role.getId());
                roleAuth.setUpdateUser(loginUser.getUser().getId());
                shopRoleAuthMapper.insert(roleAuth);
            }

        }
    }

    public void updateRole(ShopRoleUpdateRequest req, LoginUserVo loginUser) {
        // 校验role存在
        ShopRoleEntity role = shopRoleMapper.selectById(req.getId());
        AssertUtil.notNull(role, "角色不存在");
        if (StringUtils.isNotBlank(req.getRoleName())) {
            ShopRoleEntity roleUpdate = new ShopRoleEntity();
            roleUpdate.setId(role.getId());
            roleUpdate.setName(req.getRoleName());
            roleUpdate.setUpdateUser(loginUser.getUser().getId());
            shopRoleMapper.updateById(roleUpdate);
        }
        if (StringUtils.isNotBlank(req.getAuthPaths())) {
            // 删除先前的权限
            ShopRoleAuthEntity roleAuthDel = new ShopRoleAuthEntity();
            roleAuthDel.setRoleId(role.getId());
            shopRoleAuthMapper.deleteByEntity(roleAuthDel);
            List<String> authPathList = Arrays.asList(req.getAuthPaths().split(","));
            List<ShopAuthEntity> authList = shopAuthService.getByPaths(authPathList);
            AssertUtil.isTrue(authList.size() == authPathList.size(), "权限不存在");
            for (ShopAuthEntity auth : authList) {
                ShopRoleAuthEntity roleAuth = new ShopRoleAuthEntity();
                roleAuth.setAuthId(auth.getId());
                roleAuth.setCreateUser(loginUser.getUser().getId());
                roleAuth.setRoleId(role.getId());
                roleAuth.setUpdateUser(loginUser.getUser().getId());
                shopRoleAuthMapper.insert(roleAuth);
            }
        }

    }

    public void delRole(ShopRoleDelRequest req, LoginUserVo loginUser) {
        ShopRoleEntity role = shopRoleMapper.selectById(req.getId());
        AssertUtil.notNull(role, "角色不存在");
        shopRoleMapper.deleteById(role.getId());
        // 删除role关联的权限
        ShopRoleAuthEntity roleAuthDel = new ShopRoleAuthEntity();
        roleAuthDel.setRoleId(role.getId());
        shopRoleAuthMapper.deleteByEntity(roleAuthDel);
        // 删除用户关联
        ShopUserRoleEntity userRoleDel = new ShopUserRoleEntity();
        userRoleDel.setRoleId(role.getId());
        shopUserRoleMapper.deleteByEntity(userRoleDel);
    }

}
